/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DLL;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.nio.channels.DatagramChannel;
import java.nio.charset.Charset;
import java.util.Arrays;

/**
 *
 * @author hoangvukenshinPC
 */

public class Server {
  public static void main(String[] args) throws Exception {
    int PORT = 4200;
    byte[] buf = new byte[1024];
    DatagramPacket dgp = new DatagramPacket(buf, buf.length);
    DatagramSocket sk;
    
    //Send data
    byte[] bufSe = new byte[1024];

    sk = new DatagramSocket(PORT);
    System.out.println("Server started");
    while (true) {
      sk.receive(dgp);
      byte[] reBuf = dgp.getData();
      byte a = reBuf[0];
      byte[] dataB = Arrays.copyOfRange(reBuf, 1, reBuf.length);
      String dataP = dataB.toString();
      System.out.println("Receive " + new String(dataB));
      System.out.println(reBuf.length);
      BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
      String outMessage = stdin.readLine();
      if(outMessage.equals("-1"))
      {
          byte[] flag = new byte[]{1};
          flag = "2".getBytes();
          String tenFile1 = "PhimMa.1";
          String tenFile2 = "PhimMa.2";
          String listFile = "";
          listFile = listFile + tenFile1;
          listFile = listFile + ",";
          listFile = listFile + tenFile2;
          //Neu khong co fiel thi cho listFile = "KhongCo" rui ben nhan check;
          byte[] listFileByte = listFile.getBytes();
          System.arraycopy(flag, 0, bufSe, 0, flag.length);
          System.arraycopy(listFileByte, 0, bufSe, flag.length, listFileByte.length);
          //System.arraycopy(tenFileByte, 0, buf, flag.length, tenFileByte.length);
      }
      DatagramPacket out = new DatagramPacket(bufSe, bufSe.length, dgp.getAddress(), dgp.getPort());
      sk.send(out);
    }
  }
}
